package edu.sziit;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class GeneratorMain {
    public static void main(String[] args) {
        //创建generator对象
        AutoGenerator autoGenerator=new AutoGenerator();
        //配置数据源
        DataSourceConfig dataSourceConfig=new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        dataSourceConfig.setUrl("jdbc:mysql://47.115.47.67:3306/mall?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");
        autoGenerator.setDataSource(dataSourceConfig);
        //全局配置
        GlobalConfig globalConfig=new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir")+"./v-manager-backend/src/main/java");//输出路径：System.getProperty("user.dir")表示当前项目
        globalConfig.setOpen(false);
        //%s与表名映射
        globalConfig.setServiceName("%sService");//默认生成的是IxxxService
        globalConfig.setControllerName("%sController");//默认生成的是xxxController
        autoGenerator.setGlobalConfig(globalConfig);
        //配置包信息
        PackageConfig packageConfig=new PackageConfig();
        packageConfig.setParent("edu.sziit.manager_application");//设置父包
//        packageConfig.setModuleName("1");//包里面创建新的包，自动生成的都在这里面
        packageConfig.setController("controller");//设置controller生成的文件夹，以下同理
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mappers");
        packageConfig.setEntity("pojo");
        autoGenerator.setPackageInfo(packageConfig);
        //配置生成策略
        StrategyConfig strategyConfig=new StrategyConfig();
        //strategyConfig.setInclude("xxx1","xxx2");默认自动生成数据库全部表，如果是生成部分表的话，就在里面添加对应的表名就行了，参数可变
        strategyConfig.setEntityLombokModel(true);//实体类添加Lombok，并有@Data注解
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);//42、43都要，user_name-->userName
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        autoGenerator.setStrategy(strategyConfig);

        //运行
        autoGenerator.execute();
    }
}
